package com.oa.ylms.apps.dao.goods.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.goods.IGoodsOrderDetailDao;
import com.oa.ylms.apps.entity.goods.GoodInfoValue;
import com.oa.ylms.apps.entity.goods.GoodOrderDetailValue;
import com.oa.ylms.apps.entity.goods.GoodOrderValue;

public class GoodsOrderDetailDaoImpl extends HibernateDaoSupport implements IGoodsOrderDetailDao {
	List<GoodOrderDetailValue> list;
	@Override
	public int addOrderDetail(GoodOrderDetailValue orderDetail)
			throws Exception {
		if (orderDetail != null) {
			this.getHibernateTemplate().save(orderDetail);
			return orderDetail.getDetailId();
		}
		return 0;
	}
	@Override
	public boolean modifyOrderDetail(GoodOrderDetailValue orderDetail)
			throws Exception {
		if (orderDetail != null) {
			try {
				this.getHibernateTemplate().update(orderDetail);
			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<GoodOrderDetailValue> queryByOrderId (GoodOrderDetailValue orderDetail) throws Exception{
		String hql = "FROM GoodOrderDetailValue godv WHERE godv.goodOrder =? ";
		return this.getHibernateTemplate().find(hql,orderDetail.getGoodOrder());
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public GoodOrderDetailValue queryByOrderIdAndGoodId(final GoodInfoValue goodInfo,
			final GoodOrderValue goodOrder) throws Exception {
		final String hql = "FROM GoodOrderDetailValue godv WHERE godv.goodOrder =? AND godv.goodInfo = ?";
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setEntity(0, goodOrder);
				query.setEntity(1, goodInfo);
				list = query.list();
				return list;
			}
		});
		return list.get(0);
	}
	
	
	public List<GoodOrderDetailValue> getList() {
		return list;
	}
	public void setList(List<GoodOrderDetailValue> list) {
		this.list = list;
	}
}
